package com.example.translator;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

public class ConfigDialog extends JDialog {
    private JTextField baseUrlField = null;
    private JTextField apiKeyField = null;
    private JTextField modelField = null;
    private JTextField temperatureField = null;
    private JTextField maxTokensField = null;
    private final JCheckBox useCustomConfig;
    private boolean confirmed = false;

    public ConfigDialog(Frame owner, Properties defaultConfig) {
        super(owner, "OpenAI 配置", true);
        setSize(400, 300);
        setLocationRelativeTo(owner);

        // 创建主面板
        JPanel mainPanel = new JPanel(new GridBagLayout());
        mainPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(5, 5, 5, 5);
        gbc.fill = GridBagConstraints.HORIZONTAL;

        // 使用自定义配置的复选框
        useCustomConfig = new JCheckBox("使用自定义配置");
        useCustomConfig.addActionListener(e -> {
            boolean enabled = useCustomConfig.isSelected();
            baseUrlField.setEnabled(enabled);
            apiKeyField.setEnabled(enabled);
            modelField.setEnabled(enabled);
            temperatureField.setEnabled(enabled);
            //maxTokensField.setEnabled(enabled);
        });

        // base-url
        gbc.gridx = 0;
        gbc.gridy = 0;
        mainPanel.add(new JLabel("base-url:"), gbc);
        gbc.gridx = 1;
        baseUrlField = new JTextField(20);
        baseUrlField.setEnabled(false);
        mainPanel.add(baseUrlField, gbc);


        // API Key
        gbc.gridx = 0;
        gbc.gridy = 1;
        mainPanel.add(new JLabel("API Key:"), gbc);
        gbc.gridx = 1;
        apiKeyField = new JTextField(20);
        apiKeyField.setEnabled(false);
        mainPanel.add(apiKeyField, gbc);

        // Model
        gbc.gridx = 0;
        gbc.gridy = 2;
        mainPanel.add(new JLabel("Model:"), gbc);
        gbc.gridx = 1;
        modelField = new JTextField(20);
        modelField.setEnabled(false);
        mainPanel.add(modelField, gbc);

        // Temperature
        gbc.gridx = 0;
        gbc.gridy = 3;
        mainPanel.add(new JLabel("Temperature:"), gbc);
        gbc.gridx = 1;
        temperatureField = new JTextField(20);
        temperatureField.setEnabled(false);
        mainPanel.add(temperatureField, gbc);

        // Max Tokens
//        gbc.gridx = 0;
//        gbc.gridy = 3;
//        mainPanel.add(new JLabel("Max Tokens:"), gbc);
//        gbc.gridx = 1;
//        maxTokensField = new JTextField(20);
//        maxTokensField.setEnabled(false);
//        mainPanel.add(maxTokensField, gbc);

        // 按钮
        JPanel buttonPanel = new JPanel(new FlowLayout());
        JButton okButton = new JButton("确定");
        okButton.addActionListener(e -> {
            confirmed = true;
            dispose();
        });
        JButton cancelButton = new JButton("取消");
        cancelButton.addActionListener(e -> dispose());

        buttonPanel.add(okButton);
        buttonPanel.add(cancelButton);

        // 添加组件到对话框
        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
        topPanel.add(useCustomConfig);

        add(topPanel, BorderLayout.NORTH);
        add(mainPanel, BorderLayout.CENTER);
        add(buttonPanel, BorderLayout.SOUTH);

        // 设置默认值
        if (defaultConfig != null) {
            baseUrlField.setText(defaultConfig.getProperty("openai.base-url","https://api.qnaigc.com/v1"));
            apiKeyField.setText(defaultConfig.getProperty("openai.api-key", "sk-50ce12aa059c9c464227bef024df7685679ed9beb0914a9a105985be9d8a0b46"));
            modelField.setText(defaultConfig.getProperty("openai.model", "z-ai/glm-4.6"));
            temperatureField.setText(defaultConfig.getProperty("openai.temperature", "0.7"));
        }
    }

    public boolean isConfirmed() {
        return confirmed;
    }

    public boolean isUseCustomConfig() {
        return useCustomConfig.isSelected();
    }

    public Properties getConfig() {
        Properties config = new Properties();
        if (isUseCustomConfig()) {
            config.setProperty("openai.api-key", apiKeyField.getText());
            config.setProperty("openai.model", modelField.getText());
            config.setProperty("openai.temperature", temperatureField.getText());
            config.setProperty("openai.base-url", baseUrlField.getText());
        }
        return config;
    }
}
